<script>
	var iosSessionId = 0;
	import Vue from 'vue'
	import {getUserLogin} from '@/api/index.js'
	export default {
		async onLaunch() {
			var that = this
			
			
			const urlParams = new URLSearchParams(window.location.search);
			let token = urlParams.get('temptoken');
			if(token){
				uni.setStorageSync('temptoken',token)
			}else{
				token = uni.getStorageSync('temptoken')
			}
			if (token) {
				const res = await getUserLogin({user_token:token})
				// let obj = JSON.parse(res.data)
				uni.setStorageSync('userinfo', {
					token: res.data.data.im_token,
					auth_token: res.data.data.auth_token,
					login_token:res.data.data.token
				})
				uni.setStorageSync('userInfo', res.data.data)
				uni.showToast({
					icon:'none',
					title:'登录成功'
				})
			}
			
			// uni.connectSocket({
			// 	url: `wss://gsxy1.mooyui.net:8282?user_token=${token}`,
			// 	header: {
			// 		'content-type': 'application/json'
			// 	},
			// 	success: () => {

			// 	}
			// });
			// #ifdef APP-PLUS
			plus.push.addEventListener('click', function(msg) {
				// console.log('用户点击了', msg)
				// 安卓离线通知被点击可能会触发两次click,第一次的msg.payload为莫名obj
				if ((msg.payload && Object.prototype.toString.call(msg.payload) !== '[object Object]') ||
					iosSessionId) {
					let messageShow = () => {
						let sessionId = (iosSessionId ? iosSessionId : msg.payload)
						if (parseInt(sessionId) == 0) {
							uni.switchTab({
								url: '/pages/message/message'
							})
							return;
						}
						uni.navigateTo({
							url: '/pages/session-info/session-info?id=' + sessionId
						})
					}
					let pages = getCurrentPages(),
						page = pages[pages.length - 1]
					if (page && page.route == 'pages/message/message') {
						messageShow()
					} else {
						that.ws.messageReady = messageShow
					}
				}
			}, false)

			let platform = that.ws.userPlatform ? that.ws.userPlatform : uni.getSystemInfoSync().platform
			if (platform == 'ios') {
				plus.push.addEventListener("receive", function(msg) {
					if ('ignore' == msg.payload) {

					} else {
						//接收透传消息
						iosSessionId = msg.payload;
						plus.push.createMessage(msg.content, 'ignore', {
							title: msg.title,
							cover: false
						});
					}
				}, false);
			}
			// #endif

			uni.onNetworkStatusChange(function(res) {
				if (res.isConnected) {
					const userinfo = uni.getStorageSync('userinfo');
					if (userinfo) {
						that.ws.init(userinfo.token, userinfo.auth_token)
					}
				} else {
					that.ws.socketTask.close()
					that.ws.socketOpen = false
					that.ws.checkNetwork()
				}
			});
		},
		onShow: function(query) {
			let that = this
			// #ifdef APP-PLUS
			plus.runtime.setBadgeNumber(0);
			// #endif
			// uni.onSocketMessage(function(res) {
			// 	let obj = JSON.parse(res.data)
			// 	if (obj.type == 'login') {
			// 		uni.setStorageSync('userinfo', {
			// 			token: obj.data.im_token,
			// 			auth_token: obj.data.auth_token
			// 		})
			// 		uni.setStorageSync('userInfo', obj.data)
			// 		if (query.path != 'pages/center/login' && query.path != 'pages/center/register') {
			// 			that.ws.pageRefresh.message = true
			// 			that.ws.pageRefresh.sessionInfo = true
			// 			that.checkLogin()
			// 		}
			// 		uni.reLaunch({
			// 		    url: '/pages/views/home' // 替换为你的页面路径
			// 		});
			// 	} else if (obj.type == 'online') {
			// 		that.$store.dispatch('handleSocketMessage', obj.msg);
			// 	}
			// });
		},
		methods: {
			checkLogin: function() {
				const userinfo = uni.getStorageSync('userinfo');
				var valid = true;
				if (!userinfo || !userinfo.token) {
					valid = false;
				} else {
					let token = userinfo.token.split('|');
					let time = Date.parse(new Date()).toString();
					time = time.substr(0, 10);
					// 减去一秒,防止刚好到时间造成发送了错误的请求
					if ((parseInt(token[2]) - 2) < parseInt(time)) {
						valid = false;
					}
				}

				if (!valid) {
					setTimeout(() => {
						this.ws.logout()
					}, 300)
				} else {
					this.ws.init(userinfo.token, userinfo.auth_token)
				}
			}
		}
	}
</script>

<style lang="scss">
	@import "uview-ui/index.scss";

	.user-status-0 {
		color: $--gray !important;
	}

	.user-status-1 {
		color: #21E589 !important;
	}

	.user-status-2 {
		color: #FF647E !important;
	}

	.user-status-3 {
		color: #FF9C4E !important;
	}

	.im-data-none {
		display: block;
		line-height: 100rpx;
		text-align: center;
		font-size: 28rpx;
		color: $--gray;
	}
</style>